package com.diyphonegadgets.freeblog.ui.fragment;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.diyphonegadgets.freeblog.R;
import com.diyphonegadgets.freeblog.activity.DIYPhoneGadgetsBlogActivity;
import com.diyphonegadgets.freeblog.data.manager.DataManager;
import com.diyphonegadgets.freeblog.data.object.Article;

public class ArticleFragment extends SherlockFragment {
	protected int mNum;
	protected DataManager mDataManager;
	private static ArticleFragment f;
	private WebView webView;
	private static Article mArticle;

	/**
	 * Create a new instance of ArtistListFragment, providing "num" as an
	 * argument.
	 */
	public static ArticleFragment newInstance(int num, Article article) {
		f = new ArticleFragment();
		mArticle=article;
		// Supply num input as an argument.
		Bundle args = new Bundle();
		args.putInt("num", num);
		f.setArguments(args);

		return f;
	}

	/**
	 * When creating, retrieve this instance's number from its arguments.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mNum = getArguments() != null ? getArguments().getInt("num") : 1;
		//setHasOptionsMenu(true);
		mDataManager = DataManager.getInstance();

	}

	/**
	 * The Fragment's UI is just a simple text view showing its instance number.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater
				.inflate(R.layout.article_detail, container, false);

		if(mArticle!=null){
		initWebView(v);
		}
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {

		super.onActivityCreated(savedInstanceState);
		setHasOptionsMenu(true);

	}
	
	
	
	void initWebView(View mainView){
		
		SimpleDateFormat sdf = new SimpleDateFormat("EEE d MMM \t hh:mm aa");

		
		
/*
		TextView titleTv = (TextView) mainView.findViewById(R.id.titleTextView);

		String title = "";
		title = "\n" + mArticle.getTitle() + "\n\t\t"
				+ sdf.format(mArticle.getPubDate()) + " \n";

		String content = "";
		content += "\n" + mArticle.getDescription() + "\n"
				+ mArticle.getLink();

		titleTv.setText(title);*/
		
		 webView=(WebView)mainView.findViewById(R.id.webkit);
		 webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		 webView. getSettings().setJavaScriptEnabled (true);
		 webView. getSettings().setPluginsEnabled (true);
		 webView. getSettings().setSupportZoom (false);
		 webView. setVerticalScrollBarEnabled (true);
		 webView. setHorizontalScrollBarEnabled (false);
		 webView.loadUrl("file:///android_asset/index.html");
				    
		     InputStream input = null;
			try {
				input = this.getActivity().getAssets().open("index.html");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	            // myData.txt can't be more than 2 gigs.
	            int size = 0;
				try {
					size = input.available();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            byte[] buffer = new byte[size];
	            try {
					input.read(buffer);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	            try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	 
	            // byte buffer into a string
	            String text = new String(buffer);


			    
			    text=text.replace("BODY_OF_ARTICLE_YAN", mArticle.getDescription());
			    text=text.replace("TITLE_OF_ARTICLE_YAN", mArticle.getTitle());
			    text=text.replace("TIME_OF_ARTICLE_YAN",  sdf.format(mArticle.getPubDate()));
			    
			    String page="<html><body>"
	            +text
	            +"</body></html>";
	    
			    
			    Log.d("DESCIPTION", mArticle.getDescription());

	    
		          webView.loadDataWithBaseURL("x-data://base", page,
		                                        "text/html", "UTF-8",
		                                        null);
		          
					DIYPhoneGadgetsBlogActivity.getFragmentInterface().setArticleTitle(mArticle.getTitle());
					DIYPhoneGadgetsBlogActivity.getFragmentInterface().setArticleURL(mArticle.getLink());


	}
	

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
	    // TODO Add your menu entries here
	    super.onCreateOptionsMenu(menu, inflater);
	    
	    inflater.inflate(R.menu.share_article_action_provider, menu);
	}
	

}